﻿lUnitFrame = LibStub("AceAddon-3.0"):NewAddon("lUnitFrame", "AceEvent-3.0")
--local AceConfig = LibStub("AceConfig-3.0")
--local ACD = LibStub("AceConfigDialog-3.0")
--local L = LibStub("AceLocale-3.0"):GetLocale("lUnitFrame")

local LUF = lUnitFrame
local db

local optGetter, optSetter
do
	function optGetter(info)
		local key = info[#info] 
		return LUF.db.profile[key]
	end
	
	function optSetter(info, value)
		local key = info[#info]
		LUF.db.profile[key] = value
		--LUF:Refresh()
	end
end


LUF.frames = {
	["player"] = PlayerFrame;
	["target"] = TargetFrame;
	["focus"]  = FocusFrame;
	["party1"] = PartyMemberFrame1,
	["party2"] = PartyMemberFrame2,
	["party3"] = PartyMemberFrame3,
	["party4"] = PartyMemberFrame4,
	}
function LUF:Print(t)
DEFAULT_CHAT_FRAME:AddMessage(t)
end
local options ,moduleOptions  = nil,{}
local function getoptions()
	if not options then 
		options =	{
			type = "group",
			name = "lUnitFrame",
			args = {
				enable = {
					type = "toggle",
					name = "Enable",
					get = function() return db.enable end;
					set = function(info , v) db.enable = v
					--LUF:Print(tostring(db.enable)..":"..tostring(v)) 
					end;
				},
			},
		}	
	end
	for key, val in pairs(moduleOptions) do
		options.args[key] = (type(val) == "function") and val() or val
	end
	return options
end
function LUF:OnEnable()
self:Print("lUnitFrame v0.1 is loaded")
end

function LUF:OnInitialize()
	self.db = LibStub("AceDB-3.0"):New("LUFDB")
    self.db:RegisterDefaults({
        profile = {
            enable = true;
            modules = {
              ['*'] = true,
            },
        },
    })
	db = self.db.profile
	self:BuildConfig();
end

function LUF:BuildConfig()
	
	LibStub("AceConfigRegistry-3.0"):RegisterOptionsTable("lUnitFrame", getoptions)
	--LibStub("AceConfigDialog-3.0"):AddToBlizOptions("lUnitFrame", "lUnitFrame")
	makeSlashCmd("/luf","/lUnitFrame")
	self:RegisterModuleOptions("Profiles", LibStub("AceDBOptions-3.0"):GetOptionsTable(self.db))
end

function LUF:RegisterModuleOptions(name, optionTbl)
	moduleOptions[name] = optionTbl
end

function makeSlashCmd(...)
	local global = string.upper(LUF:GetName())
	SlashCmdList[global] = function() LibStub("AceConfigDialog-3.0"):Open("lUnitFrame") end
	for i = 1,select("#",...) do
		local slash = select(i,...)
		setglobal("SLASH_"..global..i,slash)
	end
end

function LUF:MoveFrame(frame,x,y)
--frame:ClearAllPoints()
--frame:SetPoint()
end






